package cn.com.dhcc.ddi.stat.dao;

import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import cn.com.dhcc.app.core.dao.BaseDao;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.pub.CM;
import cn.com.dhcc.app.pub.core.util.dateformatter.DateFormatter;
import cn.com.dhcc.ddi.stat.vo.DataSyncStatVo;
import cn.com.dhcc.ddi.stat.vo.wrapper.DataSyncStatRowWrapper;
/**
 * 
    * @ClassName: DataSyncReceiveStatDao
    * @author 18315
    * @date 2017-3-10
    *
 */
@Repository
public class DataSyncReceiveStatDao extends BaseDao {
	
	private static Logger logger = Log.getLogger(Log.LOGTYPE.DDI);
	
	/**
	 * 
	    * @Title: getSendInfo
	    * @Description: 获取指定时间段内的接受数据量/频次统计数据
	    * @param @param start
	    * @param @param end
	    * @param @return
	    * @param @throws DaoException    参数
	    * @return List<DataSyncStatVo>    返回类型
	    * @throws
	 */
	public List<DataSyncStatVo> getReceiveInfo(String start, String end) throws DaoException {
		String msg = "";
		String sql = "select node_id, cata_id, TOTAL, SUM(TOTAL) AS sum, data_size, SUM(data_size) AS num FROM " 
					  + SI_TABLE("t_ddi_data_sync_statistics") 
					  + " WHERE stat_date BETWEEN ? AND ?" 
					  + " AND exch_type= 1 "
					  + " GROUP BY node_id";
		try {
			return CM.getDao().list("发送数据信息", sql, new DataSyncStatRowWrapper(), new Object[] {start, end});
		} catch (DaoException e) {
			msg = "每天发送数据统计失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
	 * 计算每日接收总量
	 * @return 
	 * @throws DaoException
	 */
	public List<DataSyncStatVo> CalculateDailyReceiveStatistics() throws DaoException {
		String msg = "";
		String sql =  "SELECT DISTINCT  t4.node_id as node_id,t2.date_category_id as cata_id,'1' as exch_type,COUNT(t1.id) AS sum, SUM(t1.total) AS num FROM "
	              + SI_TABLE("t_ddi_task_log")+" t1"
	              +  " JOIN " +SI_TABLE ("t_ddi_task")+" t2 "
	              +  "  ON (t1.task_id = t2.id)"
	              +  " JOIN " +SI_TABLE("T_DDI_DS_MAPPING")+" t3 "
	              +  " ON (t3.id = t2.mp_id)"
			      +  " JOIN " +SI_TABLE(" t_ddi_datasource")+" t4 "
	              +  " ON (t4.id = t3.src_ds_id)"
			      +  " JOIN " +SI_TABLE ("t_ddi_node_info")+" t5 "
	              +  " ON (t5.id = t4.node_id)"
			      +  " JOIN " +SI_TABLE ("t_ddi_data_catagory ")+" t6 "
	              +  " ON (t6.id = t2.date_category_id)"	
	              +  " WHERE date_format(t1.mtime,'%Y-%m-%d') like ? "
	              +  " AND t1.LOG_STATUS=1 "
	              +  " AND t1.LINKTYPE=3 "
	              +  " GROUP BY t6.id";
		
		String date = DateFormatter.getDateBasedOnToday("yyyy-MM-dd", -1) + "%";
		try {
			return CM.getDao().list("获取每日发送数据统计", sql, new DataSyncStatRowWrapper(), new Object[] {date});
		} catch (DaoException e) {
			msg = "每天发送数据统计失败";
			logger.error(msg, e);
			throw new DaoException(msg, e);
		}
	}
	
	/**
     * 新增
     * @param obj
     * @return
     * @throws DaoException
     */
    public int insert(DataSyncStatVo obj) throws DaoException {
        return getDao().insertModule("新增",obj);
    }
	
    /**
     * 修改
     * @param obj
     * @return
     * @throws DaoException
     */
    public int update(DataSyncStatVo obj) throws DaoException {
        return getDao().updateModuleByIdExecuteNull("修改",obj);
    }

}
